
#include "curl_client.h"

void main()
{

}

int curl_http_post()
{

    // 初始化
    CURLcode curl_code = curl_global_init(CURL_GLOBAL_ALL);
    if (curl_code != CURLE_OK) {
        fprintf(stderr, "curl_global_init failed\n");
        return -1;
    }

    CURL *curl = curl_easy_init();
    if (curl == NULL) {
        fprintf(stderr, "curl_easy_init failed\n");
        curl_global_cleanup();
        return -1;
    }


    char *url = "http://192.168.10.1:8081/";
    char *buf = "0123456789abcdef0123456789abcdef\n";
    struct curl_slist *headers = NULL;
    headers = curl_slist_append(headers, "Content-Type:application/json;charset=UTF-8");


    /**
     * 回调函数：调试，读写功能
     * 参数列表：CURL *curl, curl_infotype type, char *info, size_t size, void *temp
     * 传递参数：最后一个参数void *info
     */
    //curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, curl_http_debug_func);
    //struct curl_http_debug_data debug_data;
    //debug_data.hex_str = 0;
    //curl_easy_setopt(curl, CURLOPT_DEBUGDATA, &debug_data);

    //显示请求响应过程
    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);

    // curl -k https ssl 跳过身份验证
    //curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
    //curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);

    // post
    curl_easy_setopt(curl, CURLOPT_POST, 1);
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);

    // 地址url
    curl_easy_setopt(curl, CURLOPT_URL, url);

    // buf
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, buf);//数据data
    curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen(buf));//大小size


    // 文件读取数据
    //FILE *read_file = fopen("/home/chench/Desktop/clion/ssl/txt.txt", "rb");
    //int read_size = 16;
    //char *read_text = malloc(read_size);
    //fgets(read_text, read_size, read_file);

    //int len = 16, times = 2;
    //char *read_text = malloc(len * times);
    //fread(read_text, len, times, read_file);
    //printf("read_text:%s\n", read_text);


    // READFUNCTION和READDATA的例子验证失败
    //curl_easy_setopt(curl, CURLOPT_READFUNCTION, curl_http_read_func);
    //curl_easy_setopt(curl, CURLOPT_READDATA, &read_file);

    //curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_http_write_func);//
    //struct curl_http_write_data write_data;
    //curl_easy_setopt(curl, CURLOPT_WRITEDATA, &write_data);

    curl_code = curl_easy_perform(curl);//执行
    if (curl_code!= CURLE_OK){
        //只有在执行完成之后才有结果
        //printf("text:%s\n", write_data.text);
    }


    //释放资源
    curl_easy_cleanup(curl);
    curl_global_cleanup();


    return 0;
}





int curl_http_debug_func(CURL *curl, curl_infotype type,
                         char *info, size_t size, void *temp)
{
    //printf("curl_http_debug\n");
    (void) curl;
    struct curl_http_debug_data *data = (struct curl_http_debug_data *) temp;
    int flag = data->hex_str;


    switch (type) {//类型判断
        case CURLINFO_TEXT:
            fprintf(stderr, "== Http Text: %s", info);
        case CURLINFO_HEADER_OUT:
            info = "=> Send header";
            break;
        case CURLINFO_DATA_OUT:
            info = "=> Send data";
            break;
        case CURLINFO_SSL_DATA_OUT:
            info = "=> Send SSL data";
            break;
        case CURLINFO_HEADER_IN:
            info = "<= Recv header";
            break;
        case CURLINFO_DATA_IN:
            info = "<= Recv data";
            break;
        case CURLINFO_SSL_DATA_IN:
            info = "<= Recv SSL data";
            break;
        default:
            return 0;
    }

    size_t i, j;
    unsigned int len = 0x00;
    if (flag) {
        len = 0x10;
    } else {
        len = 0x30;
    }

    fprintf(stderr, "%s, %10.10ld bytes (0x%8.8lx)\n",
            info, (long) size, (long) size);

    for (i = 0; i < size; i += len) {
        fprintf(stderr, "%4.4lx: ", (long) i);

        if (flag) {//显示十六进制数据
            for (j = 0; j < len; j++) {
                if (i + j < size) {
                    fprintf(stderr, "%02x ", info[i + j]);
                } else {
                    fputs("   ", stderr);
                }
            }
        } else {//显示字符串数据
            for (j = 0; (j < len) && (i + j < size); j++) {
                char c = (info[i + j] >= 0x20 && info[i + j] < 0x80) ? info[i + j] : '.';
                fputc(c, stderr);
            }
        }

        fputc('\n', stderr);
    }
    return 0;


}



size_t curl_http_read_func(char *text, size_t len, size_t times, void *temp)
{
    printf("curl_http_read_func\n");
    FILE *file = (FILE *)temp;
    size_t read = fread(text, len, times, file);
    fprintf(stderr, "read %d bytes from file\n", read);
    return read;
}



size_t curl_http_write_func(void *info, size_t len, size_t times, void *temp)
{
    printf("curl_http_write_func\n");
    size_t size = len * times;
    struct curl_http_write_data *data = (struct curl_http_write_data *) temp;

    char *text = realloc(data->text, data->size + size + 1);
    if (text == NULL){//内存分配失败
        return 0;
    }

    data->text = text;
    memcpy(&(data->text[data->size]), info, size);
    data->size += size;
    data->text[data->size] = 0;

    return size;
}


